package ck.tool.messenger;
  
  
import android.app.Activity;  
import android.content.ComponentName;  
import android.content.Intent;  
import android.content.ServiceConnection;  
import android.os.Bundle;  
import android.os.Handler;  
import android.os.IBinder;  
import android.os.Message;  
import android.os.Messenger;  
import android.os.RemoteException;  
import android.util.Log;  
import android.view.View;  
import android.view.View.OnClickListener;
import android.widget.TextView;  
  
public class MessengerStudy extends Activity {
		String TAG = "Binding";  
  
        TextView mCallbackText;  
  
        private boolean isBound = false;  
  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            // TODO Auto-generated method stub  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
            findViewById(R.id.bind).setOnClickListener(AOnClickListener);  
            findViewById(R.id.unbind).setOnClickListener(BOnClickListener);  
  
            mCallbackText = (TextView) findViewById(R.id.callback);  
            mCallbackText.setText("Not attached.");  
  
        }  
  
        private Handler mHandler = new Handler() {  
            @Override  
            public void handleMessage(Message msg) {  
                Log.i(TAG, "handleMessage");  
                switch (msg.what) {  
                case MessengerService.MSG_SET_VALUE:  
                    mCallbackText.setText("Received from service: " + msg.arg1);  
                    break;  
                default:  
                    super.handleMessage(msg);  
                }  
            }  
        };  
  
        /** 
         * 自己的信使 
         */  
        private Messenger mMessenger;  
          
        /** 
         * 远程服务的信使 
         */  
        private Messenger rMessenger;  
          
        private ServiceConnection connection = new ServiceConnection() {  
  
            public void onServiceConnected(ComponentName name, IBinder service) {  
                // TODO Auto-generated method stub  
                Log.i(TAG, "onServiceConnected");  
                rMessenger = new Messenger(service);  
                mMessenger = new Messenger(mHandler);  
                  
                sendMessage();  
            }  
  
  
            public void onServiceDisconnected(ComponentName name) {  
                // TODO Auto-generated method stub  
                rMessenger = null;  
            }  
        };  
        public OnClickListener AOnClickListener = new OnClickListener(){
        	
        public void onClick(View v) {  
            // TODO Auto-generated method stub  
            Intent intent = new Intent(  
                    "ck.tool.messengerstudy.app.messenger_service");  
                if (!isBound) {  
                    isBound = bindService(intent, connection, BIND_AUTO_CREATE);  
                    //isBound = true;  
                }else {
                	Log.i(TAG, "Sending Message to Others!!!");
                    sendMessage();  
                }
        }
        };
        public OnClickListener BOnClickListener = new OnClickListener() {
        
        	public void onClick(View v){
                if (isBound) {  
                    unbindService(connection);  
                    isBound = false;  
                } 
        	}
 
        };
          
        /** 
         * 使用服务端的信使向它发送一个消息。 
         */  
        private void sendMessage() {  
            // TODO Auto-generated method stub  
            Message message = Message.obtain(null, MessengerService.MSG_SET_VALUE);  
            message.replyTo = mMessenger;  
            try {  
                rMessenger.send(message);  
            } catch (RemoteException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
  
    }  